var util = require('./../../utils/util')
Page({
  data: {
    hasPhone: false,
    hasProfile: false,
    btn_clickable: true
  },

  userLogin() {
    // console.log(e)
    wx.login({
      success: (res) => {
        if (res.code) {
          //获取openid
          let data = {
            code: res.code
          }
          util.sendRequest('get/openid', 'POST', data).then(res => {
            if (res.data.code === 0) {
              let result = res.data.result;
              // console.log('ID:', result)
              // 包含 用户 openid session_key unionid
              this.setData({
                userBaseId: result,
                btn_clickable: false
              })
            }
          })
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'none'
          })
        }
      }
    })
    // console.log(e)    
  },

  getUerProfile(e) {
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        // console.log(res)
        let userBaseInfo = res.userInfo
        // console.log('userBaseInfo:', userBaseInfo)
        let data = {
          nickName: userBaseInfo.nickName,
          avatarUrl: userBaseInfo.avatarUrl,
          unionid: this.data.userBaseId.unionid
        }
        //向服务器更新及获取用户授权信息
        util.sendRequest('update/wxinfo', 'POST', data).then(res => {
          // console.log(res)
          if (res.data.code === 0) {
            let result = res.data.result
            // console.log('2', result)
            wx.setStorageSync('Authorization', result.Authorization);
            wx.setStorageSync('userInfo', result);
            this.setData({
              hasProfile: true
            })
            setTimeout(() => {
              this.switchToHome()
            }, 1000)
          } else if (res.data.code === 10) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        })
      },
    })
  },

  //跳转回主页
  switchToHome() {
    if (this.data.hasPhone && this.data.hasProfile) {
      wx.switchTab({
        url: '/pages/home/home',
        success: () => {
          wx.showToast({
            title: '授权成功！',
            icon: 'none'
          });
        }
      })
    }
  },


  getUserPhone(e) {
    // console.log(this.data.hasPhone)
    if (!this.data.hasPhone) {
      console.log(e)
      // 向服务器获取手机信息
      if (e.detail.encryptedData) {
        var getPhonedata = {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          session_key: this.data.userBaseId.session_key,
          unionid: this.data.userBaseId.unionid,
        }
        util.sendRequest('get/phone/number', "POST", getPhonedata).then(res => {
          // console.log(res)
          if (res.data.code === 0) {
            // console.log('获取手机号码成功')
            this.setData({
              hasPhone: true
            });
            setTimeout(() => {
              this.switchToHome()
            }, 1000)
          }
        })
      }
    }
  },

  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        getApp().globalData.windowHeightNotab = res.windowHeight;
        getApp().globalData.hoverHeightNotab = res.windowHeight - getApp().globalData.navHeight
      },
    })
    this.userLogin()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})